const cloud = require("@cloudbase/node-sdk");

const app = cloud.init({
    env: cloud.SYMBOL_CURRENT_ENV,
});

var db = app.database();
const _ = db.command
const AION_ZB_WQ = db.collection('AION-ZB-WQ')
const AION_ZB_FJ = db.collection('AION-ZB-FJ')
const AION_ZB_SP = db.collection('AION-ZB-SP')
const AION_ZB_TZ = db.collection('AION-ZB-TZ')

// 获取openId云函数入口函数
exports.main = async (event, context) => {

    // return await AION_ZB_WQ.get()
    var cType = event.cType
    var name = event.data.name
    var wqType = event.data.wqType
    var fjType = event.data.fjType
    var min = event.data.min
    var max = event.data.max
    var pingzhi = event.data.pingzhi
    var isTZ = event.data.isTZ //是否套装
    var TZname = event.data.TZname //套装名称

    if (!isTZ) {
        var params = {}

        if (name)
            params.name = db.RegExp({
                regexp: String(name),
                options: 'i',
            })
        if (wqType)
            params.type = wqType
        if (fjType)
            params.type = fjType
        if (pingzhi)
            params.pingzhi = pingzhi


        if (min || max)
            params.level = _.gte(min).and(_.lte(max))


        if (!cType) {
            // 查询全部
            return await AION_ZB_WQ.get()
        } else if (cType) {
            if (cType == 1) {
                // return await AION_ZB_WQ.where(params).get()
                const res = await AION_ZB_WQ.where(params)
                const list = (await res
                    .skip((event.data.pageIndex - 1) * event.data.pageSize)
                    .limit(event.data.pageSize)
                    .get()).data

                const total = (await res.count()).total

                return {
                    list: list,
                    total: total,
                }
            } else if (cType == 2) {
                const res = await AION_ZB_FJ.where(params)
                const list = (await res
                    .skip((event.data.pageIndex - 1) * event.data.pageSize)
                    .limit(event.data.pageSize)
                    .get()).data

                const total = (await res.count()).total

                return {
                    list: list,
                    total: total,
                }
            } else if (cType == 3) {
                const res = await AION_ZB_SP.where(params)
                const list = (await res
                    .skip((event.data.pageIndex - 1) * event.data.pageSize)
                    .limit(event.data.pageSize)
                    .get()).data

                const total = (await res.count()).total

                return {
                    list: list,
                    total: total,
                }
            }
        }
    }


    // 套装列表
    if (isTZ) {
        var params2 = {}
        if (TZname)
            params2.name = db.RegExp({
                regexp: String(TZname),
                options: 'i',
            })

        const res = await AION_ZB_TZ.where(params2)
        
        const list = (await res
            .skip((event.data.pageIndex - 1) * event.data.pageSize)
            .limit(event.data.pageSize)
            .get()).data

        const total = (await res.count()).total

        return {
            list: list,
            total: total,
        }
    }
};